<?php
namespace app\admin\controller\sum\store;

use addons\sum\service\store\Agent as StroeService;
use think\Db;
use think\exception\HttpResponseException;
use addons\sum\exception\SumException;
use app\admin\model\sum\store\Store as StoreModel;

/**
 * 商户管理
 */
class Store extends \app\admin\controller\sum\Common
{

    public function _initialize()
    {
        \app\admin\controller\sum\Common::_initialize();
        $this->model = new StoreModel;
    }

    /**
     * 商户申请列表
     */
    public function index()
    {
        $map=[];
        if (!$this->auth->isSuperAdmin()) {
            $admin=$this->auth->getUserInfo();
//            if(!$admin['store_id']){
//                $this->error('请绑定商户');
//            }
//            $map['id']=$admin['store_id'];
        }

        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }
//        $records = $this->model->with(['user','agent'])->where($map)->sheepFilter()->paginate($this->request->param('list_rows', 10));
        $records = $this->model->with(['user','agent'])->sheepFilter()->paginate($this->request->param('list_rows', 10));
        $this->success('获取成功', null, $records);
    }
    /**
     * 添加
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }
        $params = $this->request->only([
            'image', 'store_name','user_name', 'phone', 'dian','status',
            'province_name','city_name','district_name','province_id','city_id','district_id'
        ]);
        if(isset($params['dian'])&&is_array($params['dian'])){
            $params['dian']=json_encode($params['dian']);
        }
        $this->svalidate($params, '.add');

        $this->model->save($params);

        $this->success('保存成功', null, $this->model);
    }

    /**
     * 详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->find();
        if (!$detail) {
            $this->error(__('No Results were found'));
        }
        $detail['dian']=json_decode($detail['dian'],true);
        $this->success('获取成功', null, $detail);
    }


    /**
     * 编辑(支持批量)
     */
    public function edit($id = null)
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch('add');
        }

        $params = $this->request->only([
            'image', 'store_name','user_name', 'phone', 'dian','status',
            'province_name','city_name','district_name','province_id','city_id','district_id'
        ]);

        $list = $this->model->where('id', 'in', $id)->select();
        $result = Db::transaction(function () use ($list, $params) {
            $count = 0;
            foreach ($list as $item) {
                $params['id'] = $item->id;
                if(isset($params['dian'])&&is_array($params['dian'])){
                    $params['dian']=json_encode($params['dian']);
                }
                $this->svalidate($params);
                $count += $item->save($params);
            }
            return $count;
        });

        if ($result) {
            $this->success('更新成功', null, $result);
        } else {
            $this->error('更新失败，未改变任何记录');
        }
    }

    /**
     * 删除(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }

        $list = $this->model->where('id', 'in', $id)->select();
        $result = Db::transaction(function () use ($list) {
            $count = 0;
            foreach ($list as $item) {
                $count += $item->delete();
            }
            return $count;
        });

        if ($result) {
            $this->success('删除成功', null, $result);
        } else {
            $this->error(__('No rows were deleted'));
        }
    }

    /**
     * 评价回收站
     *
     * @return void
     */
    public function recyclebin()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

        $comments = $this->model->onlyTrashed()->sheepFilter()->paginate($this->request->param('list_rows', 10));

        $this->success('获取成功', null, $comments);
    }


    /**
     * 还原(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function restore($id = null)
    {
        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }
        $items = $this->model->onlyTrashed()->where('id', 'in', $id)->select();
        Db::transaction(function () use ($items) {
            $count = 0;
            foreach ($items as $item) {
                $count += $item->restore();
            }

            return $count;
        });

        $this->success('还原成功');
    }


    /**
     * 销毁(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function destroy($id = null)
    {
        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', 'id'));
        }
        if ($id !== 'all') {
            $items = $this->model->onlyTrashed()->whereIn('id', $id)->select();
        } else {
            $items = $this->model->onlyTrashed()->select();
        }
        $result = Db::transaction(function () use ($items) {
            $count = 0;
            foreach ($items as $comment) {
                $count += $comment->delete(true);
            }
            return $count;
        });

        if ($result) {
            $this->success('销毁成功', null, $result);
        }
        $this->error('销毁失败');
    }

    public function selectpage()
    {
        return parent::selectpage();
    }
}
